<?php

namespace app\api\controller;

use think\Controller;
use think\Request;
use app\api\model\SaleModel;
use app\api\validate\SaleValidate;
use think\Db;
use app\http\TokenTrait;

class Sale extends Common
{
    use TokenTrait;

    /**
     * 销售订单 列表
     * @return \think\response\Json
     */
    public function SaleList(Request $request, SaleModel $model)
    {
        $data = $request->param();
        $data['start_time'] = isset($data['start_time']) ? strtotime($data['start_time']) : '';
        $data['end_time'] = isset($data['end_time']) ? strtotime($data['end_time']) : '';
        $list = $model->getSaleList($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 销售订单 新增
     * @return \think\response\Json
     */
    public function SaleAdd(Request $request, SaleModel $model)
    {
        $data = $request->param();
        $data['number'] = 'jxc' . time();//单号
        $data['maker'] = self::getUId();//制单人id
        $data['create_time'] = strtotime($data['create_time']);
        $data['type1'] = 1;//1销售单
        if($data['mode'] != 0){
            $data['mode_address'] =json_encode($data['mode_address'], JSON_UNESCAPED_UNICODE);
        }
        if (isset($data['images'])) {
            $data['images'] = json_encode($data['images'], 256);
        }
        $validate = new SaleValidate();
        if (!$validate->check($data)) {
            return json(['code' => 404, 'msg' => $validate->getError()]);
        }
        $arr = $model->getSaleAdd($data);
        if (!empty($arr)) {
            return json(['code' => 200, 'msg' => '添加成功', 'data' => $arr]);
        } else {
            return json(['code' => 404, 'msg' => '添加失败']);
        }
    }

    /**
     * 销售订单  ------详情
     * @param $id 销售订单id
     * @param SaleModel $model
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function SaleDetail($id, SaleModel $model)
    {
        $list = $model->getSaleInfo($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 销售订单  ------删除
     * @param $id 销售订单id
     * @param SaleModel $model
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function SaleDel($id, SaleModel $model)
    {
        $list = $model->getSaleDel($id);
        if ($list == 2) {
            return json(['code' => 500, 'msg' => '订单已出库,无法删除']);
        } else {
            return json(['code' => 200, 'msg' => '成功', 'data' => $list]);
        }
    }

    /**
     * 销售订单  ------撤销
     * @param $id 销售订单id
     * @return \think\response\Json
     */
    public function SaleRevoke($id, SaleModel $model)
    {
        try {
            $list = $model->getSaleRevoke($id);
            if (is_string($list)) {
                return $this->returnJson(500,$list);
            } else {
                return $this->returnJson(200,'ok');
            }
        } catch (\Exception $e) {
            return $this->returnJson(500,$e->getMessage());
        }
    }

    /**
     * 销售订单  ------留言
     * @param $id 销售订单id
     * @return \think\response\Json
     */
    public function messageAdd(Request $request, SaleModel $model)
    {
        $data = $request->param();
        $data['create_time'] = time();
        $data['maker'] =self::getUId();//制单人id
        $list = $model->addMessage($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * @param Request $request
     * @param SaleModel $model
     * @return \think\response\Json
     */
    public function messageList(Request $request, SaleModel $model)
    {
        $data = $request->param();
        $res = $model->getMessageList($data);
        if (is_string($res)) {
            return $this->returnJson(500, $res);
        } else {
            return $this->returnJson(200, 'ok', $res);
        }
    }

    public function getMessage(Request $request, SaleModel $model)
    {
        $data = $request->param();
        $res = $model->getMessage($data);
        if (is_array($res)) {
            return $this->returnJson(200, 'ok', $res);
        } else {
            return $this->returnJson(500,'参数错误', $res);
        }
    }

    /**
     * 销售订单  ------留言删除
     * @param $id 留言id
     * @param SaleModel $model
     * @return \think\response\Json
     */
    public function messageDel($id, SaleModel $model)
    {
        $list = $model->MessageDel($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    /**
     * @param $id
     * @param Request $request
     * @param SaleModel $model
     * @return \think\response\Json
     */
    public function getDraft($id, Request $request, SaleModel $model)
    {
        $data = $request->param();
        $data['maker'] = self::getUId();//制单人id
        $data['type1'] = 1;//1销售单
        if($data['mode'] != 0){
            $data['mode_address'] =json_encode($data['mode_address'], JSON_UNESCAPED_UNICODE);
        }
        $data['create_time'] = strtotime($data['create_time']);
        $list = $model->getDraft($id, $data);
        if (!empty($list)) {
            return $this->returnJson(200, 'ok', $list);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    public function getPutSale($id, Request $request, SaleModel $model)
    {
        try {
            $data = $request->param();
            $data['maker'] = self::getUId();//制单人id
            $data['create_time'] = strtotime($data['create_time']);
            if($data['mode'] != 0){
                $data['mode_address'] =json_encode($data['mode_address'], JSON_UNESCAPED_UNICODE);
            }
            $data['status'] =5;
            $arr = $model->getPutStock($id, $data);
            if ($arr) {
                return $this->returnJson(200, 'success');
            } else {
                return $this->returnJson(500, 'fail');
            }
        } catch (\Exception $e) {
            return $this->returnJson(500, $e->getMessage(), 500);
        }
    }

    /**
     * 销售订单 全部出库
     * @return string|\think\response\Json
     */
    public function SaleAllDepot($id, SaleModel $model)
    {
        $list = $model->getAllDepot($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    /**
     * 销售退货  列表
     * @return string|\think\response\Json
     */
    public function returnList(Request $request, SaleModel $model)
    {
        $data = $request->param();
        $data['start_time'] = isset($data['start_time']) ? strtotime($data['start_time']) : '';
        $data['end_time'] = isset($data['end_time']) ? strtotime($data['end_time']) : '';
        $data['number'] = isset($data['number']) ? $data['number'] : '';
        $data['courier'] = isset($data['courier']) ? $data['courier'] : '';
        $list = $model->getReturnList($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 销售退货  新增
     * @return \think\response\Json
     */
    public function returnAdd(Request $request, SaleModel $model)
    {
        $data = $request->param();
        $data['maker'] = self::getUId();//制单人id
        $data['number'] = 'jxc' . time();
        $data['type'] = 4;//销售退货单
        $data['create_time'] = strtotime($data['create_time']);
        $list = $model->getReturnAdd($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 销售退货  ------详情
     * @param $id 销售退货id
     * @return \think\response\Json
     */
    public function returnDetail($id, SaleModel $model)
    {
        $list = $model->getReturnDetail($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    public function returnDepot($id, Request $request, SaleModel $model)
    {
        try {
            $data = $request->param();
            $data['status'] =1;
            $data['maker'] = self::getUId();//制单人id
            $list = $model->getReturnDepot($id, $data);
            if ($list) {
                return json(['code' => 200, 'msg' => 'success']);
            } else {
                return json(['code' => 500, 'msg' => 'fail']);
            }
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage().'-'.$e->getLine()], 500);
        }
    }

    public function returnDraft($id, Request $request, SaleModel $model)
    {
        $data = $request->param();
        $data['maker'] = self::getUId();//制单人id
        $data['create_time'] = strtotime($data['create_time']);
        $data['type'] = 4;//4销售退货单
        $list = $model->getReturnDraft($id, $data);
        if (!empty($list)) {
            return $this->returnJson(200, 'ok', $list);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 销售退货  ------撤销
     * @param $id 销售退货单id
     * @return \think\response\Json
     */
    public function returnRevoke($id, SaleModel $model)
    {
        $list = $model->getReturnRevoke($id);
        if ($list == 1) {
            return json(['code' => 500, 'msg' => '单据无法撤销']);
        } else {
            return json(['code' => 200, 'msg' => '成功']);
        }
    }

    /**
     * 销售退货  ------删除
     * @param $id 销售退货单id
     * @return \think\response\Json
     */
    public function returnDel($id, SaleModel $model)
    {
        $list = $model->getReturnDel($id);
        if ($list == 1) {
            return json(['code' => 500, 'msg' => '单据无法删除']);
        } else {
            return json(['code' => 200, 'msg' => '成功']);
        }
    }

    /**
     * 前台收银 列表
     */
    public function collectList(Request $request, SaleModel $model)
    {
        $list = $model->getCollectList($request->param());
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 前台收银 新增
     */
    public function collectAdd(Request $request, SaleModel $model)
    {
        $data = $request->param();
        $data['maker'] = self::getUId();
        $data['number'] = 'jxc' . time();
        $data['status'] = 1;
        $data['create_time'] = time();
        $list = $model->getCollectAdd($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 前台收银  ------详情
     * @param $id 销售退货id
     * @return \think\response\Json
     */
    public function collectDetail($id, SaleModel $model)
    {
        $list = $model->getCollectDetail($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 前台收银 撤销
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function collectRevoke($id, SaleModel $model)
    {
        $list = $model->getCollectRevoke($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }
}